/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.supinfo.youfood.dao.jpa;

import com.supinfo.youfood.dao.Commande_MenuDao;
import com.supinfo.youfood.entity.Commande;
import com.supinfo.youfood.entity.Commande_Menu;
import com.supinfo.youfood.entity.Menu;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Michael
 */
@Stateless
public class JpaCommande_MenuDao implements Commande_MenuDao{
    
    @PersistenceContext
    private EntityManager em;

    @Override
    public Commande_Menu addCommande_Menu(Commande c, Menu m) {
        Commande_Menu cm = new Commande_Menu(m, c);
        em.persist(cm);
        return cm;
    }

    @Override
    public List<Commande_Menu> getCommandesAF(List<Commande_Menu> lcm) {
        List<Commande_Menu> res = new ArrayList<Commande_Menu>();
        String query = "SELECT cm "+
                        "FROM Commande_Menu cm "+
                        "WHERE cm.etat = :etat";
        Query result = em.createQuery(query);
        result.setParameter("etat", 1);
        res = result.getResultList();
        return res;
    }

    @Override
    public List<Commande_Menu> getCommandesEnCours(List<Commande_Menu> lcm) {
        List<Commande_Menu> res = new ArrayList<Commande_Menu>();
        String query = "SELECT cm "+
                        "FROM Commande_Menu cm "+
                        "WHERE cm.etat = :etat";
        Query result = em.createQuery(query);
        result.setParameter("etat", 2);
        res = result.getResultList();
        return res;
    }

    @Override
    public List<Commande_Menu> getCommandesFini(List<Commande_Menu> lcm) {
        List<Commande_Menu> res = new ArrayList<Commande_Menu>();
        String query = "SELECT cm "+
                        "FROM Commande_Menu cm "+
                        "WHERE cm.etat = :etat";
        Query result = em.createQuery(query);
        result.setParameter("etat", 3);
        res = result.getResultList();
        return res;
    }

    @Override
    public Commande_Menu findCommande_MenuById(Long id) {
        return em.find(Commande_Menu.class, id);
    }

    @Override
    public void updateCommande_Menu(Commande_Menu cm) {
        em.merge(cm);
    }
}